import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/lget/lget.dart';
import 'package:liuxlcommon/view/extension_widget.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'package:love_newf/app/com/base/app.dart';
import 'package:love_newf/app/com/view/common_view.dart';
import 'package:love_newf/app/page/index/index_page.dart';
import 'package:love_newf/app/page/message_list/message_list_view.dart';
import 'package:love_newf/app/page/mine/mine_page.dart';
import 'package:love_newf/app/page/nearby/nearby_view.dart';
import 'package:love_newf/app/page/wallet/wallet_view.dart';
import 'package:love_newf/generated/assets.dart';
import 'package:love_newf/value/app_colors.dart';
import 'package:love_newf/app/com/http/http_request.dart';
import 'package:liuxlcommon/util/toast_util.dart';

import '../login/login_page.dart';
import 'home_logic.dart';

class HomePage extends StatefulWidget {
  HomePage({super.key});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with RouteAware {
  final logic = Get.put(HomeLogic());
  List<Widget> pglist = [
    const IndexPage(),
    const NearbyPage(),
    // const WalletPage(),
    const MessageListPage(),
    const MinePage()
  ]; //OrderPage(),
  var tabImages = [
    [getTabImage(Assets.tabHomeOn), getTabImage(Assets.tabHomeOff)],
    [getTabImage(Assets.tabNearbyOn), getTabImage(Assets.tabNearbyOff)],
    // [getTabImage(Assets.tabWalletOn), getTabImage(Assets.tabWalletOff)],
    [getTabImage(Assets.tabMessageOn), getTabImage(Assets.tabMessageOff)],
    [getTabImage(Assets.tabMeOn), getTabImage(Assets.tabMeOff)],
  ];

  static const tabNames = ["首页", "附近", "消息", "我的"]; //, "钱包"

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(seconds: 1)).then((s) {
      CommonView.commDialog(
          confirm: () {
            Get.back();
          }, title: "提示", message: "禁止未成年人注册使用！",isShowClose:false);
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    App.routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute);
    // precacheImage(const AssetImage(Assets.bPinjamSy3), context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: AppColors.bgColor,
      body: GetBuilder<HomeLogic>(
        builder: (s) => doubleExit(
            child: Stack(
          children: [
            LContainer(
                lStyle:
                    LStyle(width: double.infinity, height: double.infinity)),
            Column(
              children: [LExpand(child: pageView), bottomBar],
            )
          ],
        )),
      ),
    );
  }

  get bottomBar => GetBuilder<HomeLogic>(
        id: 'bottom',
        builder: (l) => LRow(
            lStyle: LStyle(
                backgroundColor: Colors.white,
                // padding: EdgeInsets.symmetric(horizontal: 50.w),
                // borderRadius: BorderRadius.only(
                //     topLeft: Radius.circular(20.w),
                //     topRight: Radius.circular(20.w)),
                boxShadow: [
                  BoxShadow(
                      color: 0x336C666A.color,
                      offset: const Offset(0, 0),
                      blurRadius: 4.w)
                ]),
            children: List.generate(pglist.length, (index) => tabItem(index))),
      );

  get pageView => PageView(
        physics: const NeverScrollableScrollPhysics(),
        controller: logic.pageController,
        onPageChanged: (position) {
          logic.mapHomeListener.forEach((key, value) {
            value.onHomePageChanged(position);
          });
        },
        children: List.generate(pglist.length, (index) => pglist[index]),
      );

  Widget tabItem(index) {
    return LColumn(
        lStyle: LStyle(height: 52.w + LGet.bottomPadding(context), flex: 1),
        children: getTabIcon(index),
        onTap: () => nitemclick(index));
  }

  getTabIcon(int curIndex) {
    Image image = curIndex == logic.currentIndex
        ? tabImages[curIndex][0]
        : tabImages[curIndex][1];
    return [
      3.lsh,
      image,
      // 3.lsh,
      LText.easy(
          tabNames[curIndex],
          10.w,
          null,
          curIndex == logic.currentIndex
              ? AppColors.textColor
              : const Color(0xFF747D82))
    ];
  }

  int onclickTime = 0;
  int count = 0;

  nitemclick(index) async {
    if (index != 0 && !App.isLogin()) {
      Get.to(LoginPage());
      return;
    }
    logic.pageController.jumpToPage(index);
    logic.currentIndex = index;
    setState(() {});

    int currTime = DateTime.now().millisecondsSinceEpoch;
    if (currTime - onclickTime < 300 || onclickTime == 0) {
      count++;
      onclickTime = currTime;
      if (count > 9) {
        HttpRequest.isProxy = !HttpRequest.isProxy;
        ToastUtils.toast(HttpRequest.isProxy.toString());
        count = 0;
      }
    } else {
      onclickTime = currTime;
      count = 0;
    }
  }

  @override
  void dispose() {
    // Get.delete<HomeLogic>();
    App.routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didPush() {}

  @override
  void didPop() {}

  @override
  void didPopNext() {
    logic.mapHomeListener.forEach((key, value) {
      value.onHomePageChanged(logic.currentIndex);
    });
  }

  @override
  void didPushNext() {}
}

Image getTabImage(String path) {
  return Image.asset(path, width: 32.w, height: 32.w);
}
